package com.datatest.riskcheck.controller;

import com.datatest.riskcheck.service.api.CenariusFeign;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;

/**
 * @Author lxt
 * Created by Jccfc on 2021/11/1.
 */
@RequestMapping("/cenarius/")
@Slf4j
@RestController
public class CenariusController {

    @Resource
    private CenariusFeign cenariusFeign;

    @PostMapping(value = "getCookie")
    public Object cookie(@RequestBody Map<String,Object> params){
        Response response = cenariusFeign.login(params.get("accessKey").toString());
        Collection<String> setCookie = response.headers().get("set-cookie");
        StringBuilder cookie = new StringBuilder();
        for (String s : setCookie){
            if (s.startsWith("csrftoken")){
                cookie.append(s).append("; ");
            }else if (s.contains("sessionid")){
                cookie.append(s.substring(s.indexOf("sessionid"))).append("; ");
            }
        }
        return cookie.toString();
    }
}

